const express = require('express');
const path = require('path');
const fs = require('fs');
const cors=require('cors')
const app = express();
app.use(cors()); 
const port = 3001; // 你可以根据需要设置不同的端口号
app.use(express.static(path.join(__dirname, 'public')));
app.get('/getImages', (req, res) => {
  const coverDir = path.join(__dirname, 'public/cover');
  const imageFiles = fs.readdirSync(coverDir).filter(file => {
      const ext = path.extname(file).toLowerCase();
      return ext === '.jpg' || ext === '.png' || ext === '.jpeg' || ext === '.gif'; 
  });

  const imageList = imageFiles.map(file => `http://192.168.101.3:${port}/cover/${file}`); // 假设服务器和Vue应用在本地开发环境，根据实际情况调整URL

  res.json({
      imageCount: imageFiles.length,
      imageList
  });
  console.log(imageFiles.length);
  console.log(coverDir);

});

app.listen(port, () => {
  console.log(`服务器在端口 ${port} 上运行`);
});
const os = require('os');
//获取本机ip
let getIPAdress = function() {
    var interfaces = os.networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}
let ip = getIPAdress();
console.log(ip);